package com.example.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.gulimall.product.service.AttrAttrgroupRelationService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;


/**
 * 属性分组
 *
 * @author zyred
 * @email zyred@163.com
 * @date 2021-08-23 10:31:18
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId) {
        PageUtils page = attrGroupService.queryPage(params, catId);
        return R.ok().put("page", page);
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrGroupRelation(@PathVariable("attrgroupId") Long attrGroupId) {
        return R.ok().put("data", this.attrService.getRelationAttr(attrGroupId));
    }

    // /product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrGroupRelation(@PathVariable("attrgroupId") Long attrGroupId,
                                 @RequestParam Map<String, Object> params) {
        return R.ok().put("page", this.attrService.noAttrGroupRelation(params, attrGroupId));
    }

    // /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R insertAttRelation(@RequestBody List<AttrGroupRelationVo> relationVo) {
        this.attrAttrgroupRelationService.saveRelationBatch(relationVo);
        return R.ok();
    }

    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAtters(@PathVariable("catelogId") Long catelogId) {
        return R.ok().put("data", this.attrGroupService.getAttrGroupWithAtters(catelogId));
    }

    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateDetails(attrGroup);
        return R.ok();
    }

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    public R delete(@RequestBody List<AttrGroupRelationVo> voList) {
        attrService.relationDelete(voList);
        return R.ok();
    }

}
